package com.quanyan.dubbodemo.job;


import com.quanyan.common.encrypt.AESCoder;
import com.quanyan.weichat.api.WeichatServiceApi;
import com.quanyan.dubbodemo.cache.api.redis.JedisCacheApi;
import com.quanyan.dubbodemo.entity.db.TbWeichatAccount;
import com.quanyan.dubbodemo.mapper.TbWeichatAccountMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by chun.tu on 16/6/03.
 */

@Component("scheduledTask")
public class ScheduledTask {

    private static Logger logger = LoggerFactory.getLogger(ScheduledTask.class);
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private Integer expiredOrderCheckTaskCount = 1;

    @Autowired
    TbWeichatAccountMapper tbWeichatAccountMapper;


    @Autowired
    private JedisCacheApi jedisCacheApi;


    /**
     * 定期刷新accessr_token 每7000秒执行一次
     */
    @Scheduled(fixedDelay = 7000 * 1000)
    public synchronized void refreshAccessTokenTask() {
        logger.info(String.format("定期刷新access_token，第%s次执行，当前时间为：%s", expiredOrderCheckTaskCount++, dateFormat.format(new Date())));
        try {
            List<TbWeichatAccount> weichatAccounts = tbWeichatAccountMapper.selectAllByType("H5");
            for (int i = 0; i < weichatAccounts.size(); i++) {

                TbWeichatAccount weichatAccount = weichatAccounts.get(i);
                String appId = weichatAccount.getAppId();
                String securityKey = AESCoder.decodeContent(weichatAccount.getSecurityKey());
                WeichatServiceApi api = new WeichatServiceApi();
                Map<String, String> tokenMap = api.refreshToken(appId, securityKey);
                if (!tokenMap.get("result").equals("true")) {
                    tokenMap = api.refreshToken(appId, securityKey);
                }

                if (tokenMap.get("result").equals("true")) {
                    jedisCacheApi.hmset("token_" + appId, tokenMap);
                }
                String token = tokenMap.get("access_token");

                Map<String, String> ticktMap = api.refreshJsApiTiket(token);
                if (!ticktMap.get("result").equals("true")) {
                    ticktMap = api.refreshJsApiTiket(token);
                }


                if (ticktMap.get("result").equals("true")) {
                    jedisCacheApi.hmset("ticket_" + appId, ticktMap);
                }
                String access_token = jedisCacheApi.hget("token_" + appId, "access_token");
                String ticket = jedisCacheApi.hget("ticket_" + appId, "ticket");
                logger.warn("access_token>>>>>>>>>>>>>: {}", access_token);
                logger.warn("ticket>>>>>>>>>>>>>:{}", ticket);
            }
        } catch (Exception e) {
            logger.error("查询access_token异常");
        }

    }
}
